package other.xsy.passlys;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.api.annotations.RequestMethod;
import com.rkhd.platform.sdk.api.annotations.RestApi;
import com.rkhd.platform.sdk.api.annotations.RestMapping;
import com.rkhd.platform.sdk.api.annotations.RestQueryParam;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.service.XoqlService;
import other.xsy.passlys.util.HCXObjectService;
import other.xsy.passlys.util.ResultVo;

import java.util.List;

/**
 * @Description: pass 自定义的api
 * @Author: 黎炎松
 * @Date: 2024/7/31 17:40
 */
@RestApi(baseUrl = "/pass/customApi")
public class PassCustomApi {
    private static HCXObjectService hcxObjectService = new HCXObjectService();

    private static XoqlService xoqlService = XoqlService.instance();

    private static final Logger logger = LoggerFactory.getLogger();

    public static void main(String[] args) {
        PassCustomApi passCustomApi = new PassCustomApi();
        ResultVo quoteData = passCustomApi.getCreateContractData(3393689823238720L, 1);
        System.out.println("quoteData = " + quoteData.getData());
    }


    /**
     * 创建合同：  获取创建合同数据信息 (页面开发不支持 xoql  搜索且有的客户没有查询某些客户的权限)
     * @param dataId
     * @param type 1:业务机会  2:报价单
     * @return
     */
    @RestMapping(value = "/getCreateContractData", method = RequestMethod.GET)
    public ResultVo getCreateContractData(@RestQueryParam(name = "dataId") Long dataId, @RestQueryParam(name = "type") int type){
        try {
            logger.info("获取创建合同数据信息  ==> 触发开始" + type + " ==> " + dataId);
            String sql = "select id,name,quotationEntityRelAccount accountId,quotationEntityRelAccount.partyAAccount__c partyAAccount," +
                    "quotationEntityRelAccount.taxRegistrationNo__c taxRegistrationNo,quotationEntityRelAccount.accountName accountName," +
                    "quotationEntityRelOpportunity opportunityId,quotationEntityRelOpportunity.opportunityName opportunityName," +
                    "quotationEntityRelOpportunity.clientName__c clientId,quotationEntityRelOpportunity.clientName__c.accountName clientName " +
                    " from quote where id =" + dataId;

            // 业务机会获取客户信息
            if (type == 1) {
                sql = "select id,id opportunityId,PMSbudgetLink__c,opportunityName,accountId,accountId.accountName accountName," +
                        "accountId clientId,accountId.accountName clientName," +
                        "accountId.partyAAccount__c partyAAccount,accountId.taxRegistrationNo__c taxRegistrationNo " +
                        " from opportunity where id = " + dataId;
            }

            List<JSONObject> records = xoqlService.query(sql, true).getRecords();
            if (records.size() <= 0) {
                logger.info("创建合同获取报价单信息  ==> 获取数据异常: 集合为空");
                return ResultVo.getFail("获取数据异常: 集合为空");
            }
            return ResultVo.getSuccess("", records.get(0));
        } catch (Exception e) {
            logger.error("创建合同获取报价单信息  ==> 获取数据异常" + e.getMessage());
            return ResultVo.getFail(e.getMessage());
        }
    }





}
